import 'package:get/get.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/utils/total_util.dart';
import 'package:xinxuan/vo/common_vo/common_seckill_detail_data_vo.dart';
import 'package:xinxuan/vo/seckill_detail_vo.dart';
import 'package:xinxuan/vo/submit_seckill_order_vo.dart';

class SeckillDetailPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();
  
  /// 抢购详情
  var seckillDetailData = CommonSeckillDetailDataVo(id: 0, price: '', title: '').obs;

  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    int id = argumentsMap['id'];
    getSeckillDetail(id);
    super.onReady();
  }

  /// 获取抢购商品详情
  getSeckillDetail(int id) async {
    SeckillDetailVo seckillDetailVo = await HttpUtilService.getSeckillDetail(id: id);
    seckillDetailData.value = seckillDetailVo.data!;
    update();
  }

  /// 提交抢购订单
  submitSeckillOrder() async {
    if (seckillDetailData.value.id! <= 0) {
      return;
    }
    SubmitSeckillOrderVo? submitSeckillOrderVo = await HttpUtilService.submitSeckillOrder(productId: seckillDetailData.value.productId!, holderId: seckillDetailData.value.id!);
    if (submitSeckillOrderVo != null) {
      TotalUtil.showSuccess('提交成功');
      commonPageController.getUserInfo(loading: false);
      Get.back();
      // Get.offAndToNamed(
      //   '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAGE}${AppRouters.SECKILL_ORDER_DETAIL_PAGE}',
      //   arguments: {
      //     'seckillOrderId': submitSeckillOrderVo.data!.id!,
      //     'isSeckillPage': true
      //   }
      // );
    }
  }
  
}